﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Rison.BusinessFacade.Helper;


namespace Rison.BusinessFacade.Common
{
    public sealed class GlobalParams
    {

        //图片的宽、高，单位px ； 图片文件大小，单位K 
        private int _maxNewsImageWidth = 400;
        private int _maxNewsImageHeight = 300;
        private int _maxNewsImageSize = 300;
        private string _newsImageUrl = "\\file\\image\\home\\";
        private string _newsImageBaseUrl = "http://localhost:9951";

        private int _maxAttachmentSize = 10;//允许上传的文件的大小限制（M）

        //允许上传的图片的后缀类型，以英文逗号间隔 .jpg,.jpeg,.gif
        private List<string> _allowImageExtension = null;

        private List<string> _weekNames = null;
        private string _physicalPath = string.Empty;

        private int _passwordLength = 8;

        private Dictionary<string, string> _infoCategoryIds = null;

        private static string _infoNewsString = string.Empty;//新闻数据库链接字符串
        private static string _onlineSurveyString = string.Empty;//在线调查数据库链接字符串
        private static string _evaluatingString = string.Empty;//网上测评数据库链接字符串
        private static string _appealString = string.Empty;//我有诉求数据库链接字符串


        private static GlobalParams _instance = null;

        private GlobalParams()
        {
            LoadWeekNames();
            ReadConfiguration();

            _physicalPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            if (!_physicalPath.EndsWith("\\"))
                _physicalPath += "\\";    
        }

        static GlobalParams()
        {
            _instance = new GlobalParams();
        }

        public static GlobalParams Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// 应用程序的物理根路径
        /// </summary>
        public string AppPhysicalPath
        {
            get { return _physicalPath; }
        }

        /// <summary>
        /// 系统用户默认密码
        /// </summary>
        public string UserDefaultPassword
        {
            get { return "111111"; }
        }

        /// <summary>
        /// 密码登录的网上测评，随机生成的密码长度
        /// </summary>
        public int PasswordLength
        {
            get { return _passwordLength; }
        }


        #region "  LoadWeekNames  "
        private void LoadWeekNames()
        {
            _weekNames = new List<string>();
            _weekNames.Add("星期日");
            _weekNames.Add("星期一");
            _weekNames.Add("星期二");
            _weekNames.Add("星期三");
            _weekNames.Add("星期四");
            _weekNames.Add("星期五");
            _weekNames.Add("星期六");
        }

        public string GetWeekName(int index)
        {
            return _weekNames[index];
        }
        #endregion


        /// <summary>
        /// 新闻数据库链接字符串
        /// </summary>
        public string NewsConnectionString
        {
            get { return _infoNewsString; }
        }
        /// <summary>
        /// 在线调查数据库链接字符串
        /// </summary>
        public string ZXDCConnectionString
        {
            get { return _onlineSurveyString; }
        }
        /// <summary>
        /// 网上测评数据库链接字符串
        /// </summary>
        public string WSCPConnectionString
        {
            get { return _evaluatingString; }
        }
        /// <summary>
        /// 我有诉求数据库链接字符串
        /// </summary>
        public string WYSQConnectionString
        {
            get { return _appealString; }
        }

        public Dictionary<string, string> InfoCategoryIds
        {
            get { return _infoCategoryIds; }
        }

        /// <summary>
        /// 首页新闻图片的站点根地址，不包含最后一个/，如：http://localhost:9951
        /// </summary>
        public string NewsImageBaseUrl
        {
            get { return _newsImageBaseUrl; }
        }

        #region "  读取webconfig中的配置信息  "
        private void ReadConfiguration()
        {
            this._maxNewsImageWidth = GetConfigParam("maxNewsImageWidth");
            this._maxNewsImageHeight = GetConfigParam("maxNewsImageHeight");
            this._maxNewsImageSize = GetConfigParam("maxNewsImageSize");

            _allowImageExtension = new List<string>();

            this._maxAttachmentSize = GetConfigParam("maxAttachmentSize");

            this._passwordLength = GetConfigParam("passwordLength");
            if (_passwordLength <= 0 || _passwordLength > 20) _passwordLength = 8;

            GetExtension(_allowImageExtension, "allowImageExtension");

            _newsImageBaseUrl = ConfigurationSettings.AppSettings["newsImageBaseUrl"];
            if (_newsImageBaseUrl.EndsWith("/"))
                _newsImageBaseUrl = _newsImageBaseUrl.Substring(0, _newsImageBaseUrl.Length - 1);

            _infoNewsString = ConfigurationSettings.AppSettings["NewsConnectionString"];
            _onlineSurveyString = ConfigurationSettings.AppSettings["ZXDCConnectionString"];
            _evaluatingString = ConfigurationSettings.AppSettings["WSCPConnectionString"];
            _appealString = ConfigurationSettings.AppSettings["WYSQConnectionString"];

            _infoCategoryIds = new Dictionary<string, string>();
            string ids = ConfigurationSettings.AppSettings["infoCategoryIds"];
            if (!string.IsNullOrEmpty(ids))
            {
                //gg,zxzl,zspy,jfdt,jfhc,jftp 公告、专项治理、站所评议、纠风动态、纠风荟萃、纠风图片
                string[] id = ids.Split(Convert.ToChar(";"));
                _infoCategoryIds.Add("gg", id[0]);
                _infoCategoryIds.Add("zxzl", id[1]);
                _infoCategoryIds.Add("zspy", id[2]);
                _infoCategoryIds.Add("jfdt", id[3]);
                _infoCategoryIds.Add("jfhc", id[4]);
                _infoCategoryIds.Add("jftp", id[5]);
            }
        }

        private int GetConfigParam(string configKey)
        {
            if (StringHelper.IsNumber(ConfigurationSettings.AppSettings[configKey]))
                return Convert.ToInt32(ConfigurationSettings.AppSettings[configKey]);
            return 0;
        }

        private void GetExtension(List<string> extensions, string configKey)
        {
            if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings[configKey]))
            {
                string[] ens = ConfigurationSettings.AppSettings[configKey].Split(Convert.ToChar(","));
                foreach (string en in ens)
                {
                    if (!extensions.Contains(en.ToLower()))
                        extensions.Add(en.ToLower());
                }
            }
        }
        #endregion



        /// <summary>
        /// 图片新闻的图片尺寸、大小属性
        /// </summary>
        public int maxNewsImageWidth
        {
            get { return _maxNewsImageWidth; }
        }
        public int maxNewsImageHeight
        {
            get { return _maxNewsImageHeight; }
        }
        public int maxNewsImageSize
        {
            get { return _maxNewsImageSize; }
        }
 

        //图片新闻的图片上传地址
        public string NewsImageUploadUrl
        {
            get { return _newsImageUrl; }
        }

        /// <summary>
        /// 允许上传的图片的后缀类型，以英文逗号间隔
        /// </summary>
        public List<string> allowedImageExtension
        {
            get { return _allowImageExtension; }
        }


        /// <summary>
        /// 允许上传的附件的大小限制（M）
        /// </summary>
        public int maxAttachmentSize
        {
            get { return _maxAttachmentSize; }
        }

        /// <summary>
        /// 附件的上传地址
        /// </summary>
        public string AttachmentUploadUrl
        {
            get { return "\\file\\attachment\\"; }
        }


    }
}
